package cn.edu.swu.dlx;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class Main {
        public static void main(String[] args) throws Exception {
            DatabaseService Service = new DatabaseService();
            Service.init();

            List<Book> books = Service.query("select * from book", new BookResultSetVisitor());
            for(Book book:books){
                System.out.println(book.getAuthor());
            }

            List<String> names = Service.query("select name from book",
                    new ResultSetVisitor() {
                        @Override
                        public List<String> visit(ResultSet resultSet) {
                            List<String> names = new ArrayList<>();
                            try {
                                while (resultSet.next()) {
                                    names.add(resultSet.getString("name"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return names;
                        }
                    }
            );
            for(String name:names){
                 System.out.println(name);
             }

        }
}
